<?php

class Page
{
    public static function jump($url)
    {
        header("Location: {$url}");
        exit();
    }

    public static function set($name, $title, $show = false, $login = true): array
    {
        global $smarty;
        $admin_title = Tool::ini('AdminTitle', '后台管理');
        $menu_show_title = $show ? $show : $title;
        $page_title = $menu_show_title === '' ? $admin_title : $admin_title . ' - ' . $menu_show_title;
        $smarty->assign('page_title', $page_title);
        $smarty->assign('admin_title', $admin_title);
        $smarty->assign('menu_name', $name);
        $smarty->assign('menu_title', $title);
        $smarty->assign('menu_show_title', $menu_show_title);
        if ($login) {
            $header_tool = Admin::$info['header_tool'];
            $auth_list = [];
            $db = Db::get();
            if ($header_tool != '') {
                $sql = "select * from `admin_auth` where `id` in ({$header_tool}) and `status` = 1 and `level` = 2;";
                $auth_list = $db->getAll($sql);
            }
            $header_tool_array = [];
            foreach ($auth_list as $item) $header_tool_array[] = [
                'url' => Tool::ap($item['path']), 'name' => $item['name']
            ];
            $sql = 'select id from `admin_auth` where `status` = 1 ';
            if (Admin::$info['is_super'] == '1') {
                $sql .= ';';
            } else {
                $admin_auth = Admin::$info['auth'];
                if ($admin_auth == '') {
                    $sql .= "and power_type = 1;";
                } else {
                    $sql .= "and (power_type = 1 or id in ({$admin_auth})) and power_type != 3;";
                }
            }
            $auth_list = $db->getAll($sql);
            $auth_id_array = [];
            foreach ($auth_list as $item) $auth_id_array[] = $item['id'];
            $auth_id_str = implode(',', $auth_id_array);
            $sql = "select * from `admin_auth` where `status` = 1 and `is_show` = 1 and `level` = 1 order by `order`;";
            $menu_list = $db->getAll($sql);
            foreach ($menu_list as $key => $item) {
                $sql = "select * from `admin_auth` where `status` = 1 and `is_show` = 1 and `level` = 2 and pid = {$item['id']} and `id` in ({$auth_id_str}) order by `order`;";
                $menu_list[$key]['children'] = $db->getAll($sql);
            }
            $admin_menu_list = [];
            foreach ($menu_list as $item) if (count($item['children'])) $admin_menu_list[] = $item;
            $sql = "select * from `admin_auth` where `status` = 1 and `level` = 3 and `id` in ({$auth_id_str});";
            $api_list = $db->getAll($sql);
            $smarty->assign('admin_menu_list', $admin_menu_list);
            $smarty->assign('header_tool', $header_tool_array);
            $smarty->assign('admin_avatar', '/assets/image/avatar/' . Admin::$info['avatar']);
            $smarty->assign('avatar_path', '/assets/image/avatar/');
            $smarty->assign('admin_username', Admin::$info['username']);
            return ['menu_list' => $menu_list, 'api_list' => $api_list];
        }
        return [];
    }
}